package com.wyp.controller;

import com.wyp.common.Result;
import com.wyp.service.UserService;
import com.wyp.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author: Oliver
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private static Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    UserService userService;

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @RequestMapping("to_login")
    public String toLogin() {
        return "login";
    }

    /**
     * 登录操作
     * 首先校验参数--进行登录--全局异常进行处理登录过程中出现的异常，返回数据
     *
     * @param userVO
     * @return
     */
    @RequestMapping("do_login")
    @ResponseBody
    public  Result<String> doLogin(HttpServletResponse response, @Valid UserVO userVO) {
        //log.info(userVO.toString());
        String token = userService.login(response, userVO);
        return Result.success(token);
    }
}
